package com.itheima.d7_commons_io;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/*
  目标：使用CommonsIO框架进行IO相关的操作。
 */
public class CommonsIOTest1 {
    public static void main(String[] args) throws IOException {
        FileInputStream f = new FileInputStream("D:\\JavaSE进阶-相关资料\\68期\\Java进阶\\课后作业\\day10\\hero.txt");
        InputStreamReader gbk = new InputStreamReader(f, "gbk");
        BufferedReader b = new BufferedReader(gbk);
        String t;

        String s1 = b.readLine();//跳过第一行
// 创建集合用于存储 英雄对象
        ArrayList<Hero> heroes = new ArrayList<>();
        while (( t= b.readLine())!= null){
        // 2.读取的数据封装成对象
            String[] split = t.split(",+");
          String name =  split[0];
          int  life = Integer.parseInt(split[1]);
          int ganta = Integer.parseInt(split[2]);
          int xiaoguo = Integer.parseInt(split[3]);
         //作为参数传到构造器
            Hero hero = new Hero(name,life,ganta,xiaoguo);
            //存到数组中
            heroes.add(hero);
        }
        //释放资源
        b.close();
        //按照值进行排序
       Collections.sort(heroes, new Comparator<Hero>() {
           @Override
           public int compare(Hero o1, Hero o2) {
               return Double.compare(o2.getScore(),o1.getScore());
           }
       });
       //打印
       heroes.stream().forEach(s-> System.out.println(s));

        FileWriter fw = new FileWriter("D:\\JavaSE进阶-相关资料\\68期\\Java进阶\\课后作业\\day10\\hero.txt", Charset.forName("GBK"));
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(s1+"新手适应值");
        bw.newLine();
        for (Hero hero : heroes) {
            bw.write(hero.getName()+","+hero.getLife()+","+hero.getGanta()+","+hero.getXiaoguo()+","+hero.getScore());
            bw.newLine();//每写一次，换一次行
        }

        bw.close();


    }

}
